package com.mywang.weitravel.modules.mine;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.mywang.weitravel.R;
import com.mywang.weitravel.model.CollectionResponse;
import com.mywang.weitravel.modules.image.GlideImageEngine;

import java.util.ArrayList;
import java.util.List;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

public class CollectionAdapter extends RecyclerView.Adapter<CollectionAdapter.CollectionHolder> {

    private List<CollectionResponse.CollectItem> dataList = new ArrayList<>();

    @NonNull
    @Override
    public CollectionHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.item_collection, viewGroup, false);
        return new CollectionHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull CollectionHolder collectionHolder, int i) {
        GlideImageEngine.getInstance().loadImage(dataList.get(i).filepath, collectionHolder.ivPhoto);
        collectionHolder.tvTitle.setText(dataList.get(i).title);
        collectionHolder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (listener != null) {
                    listener.onItemClick(dataList.get(i), i);
                }
            }
        });
    }

    @Override
    public int getItemCount() {
        return dataList.size();
    }

    public void setData(List<CollectionResponse.CollectItem> selectList) {
        dataList.clear();
        if (selectList != null && !selectList.isEmpty()) {
            dataList.addAll(selectList);
        }
        notifyDataSetChanged();
    }

    public void addData(List<CollectionResponse.CollectItem> selectList) {
        if (selectList != null && !selectList.isEmpty()) {
            dataList.addAll(selectList);
        }
        notifyDataSetChanged();
    }

    class CollectionHolder extends RecyclerView.ViewHolder {
        public ImageView ivPhoto;
        public TextView tvTitle;

        public CollectionHolder(@NonNull View itemView) {
            super(itemView);
            ivPhoto = itemView.findViewById(R.id.iv_photo);
            tvTitle = itemView.findViewById(R.id.tv_title);
        }
    }

    private OnItemOnclickListener listener;

    public void setOnItemOnclickListener(OnItemOnclickListener listener) {
        this.listener = listener;
    }

    public interface OnItemOnclickListener {
        void onItemClick(CollectionResponse.CollectItem item, int position);
    }
}
